using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms; 
  
namespace Adoor.VSDesigner
{
    public sealed class SqlConnectionStringEditor : System.ComponentModel.Design.ObjectSelectorEditor
    {

        static SqlConnectionStringEditor()
        {
            //DbConnectionStringEditor.NEW_CONNECTION_ENTRY = typeof(IDbConnection);
        }

        public SqlConnectionStringEditor()
            : base(false)
        {
        }


        public override object EditValue(ITypeDescriptorContext context, IServiceProvider serviceProvider, object value)
        {
            if (((serviceProvider != null) && (context != null)) && (context.Instance != null))
            {
                try
                {
                    context.OnComponentChanging();
                    object obj1 = base.EditValue(context, serviceProvider, value);
                    string text1 = obj1 as string;

                    //no test and comply dbConnectionType
                    if ((text1 == null) && (obj1 == DbConnectionStringEditor.NEW_CONNECTION_ENTRY))
                    {
                        VsConnectionManager manager1 = new VsConnectionManager(serviceProvider);
                        int num1 = manager1.AddNewConnection(this.ManagedProviderInvariantName);
                        if ((num1 >= 0) && (this.selector != null))
                        {
                            string text2 = manager1.GetConnectionString(num1);
                            string text3 = manager1.GetConnectionName(num1);
                            text1 = text2;
                            ObjectSelectorEditor.SelectorNode node1 = this.selector.AddNode(text3, text2, null);
                            this.selector.SelectedNode = node1;
                        }
                    }

                    if (!String.IsNullOrEmpty(text1.Trim()))
                    {
                        ConnectionString text4 = new ConnectionString(this.ManagedProviderInvariantName, text1);
                        DialogResult result1 = DataDesignUtil.PromptKeepPassword(text4, serviceProvider);
                        if (result1 == DialogResult.Cancel)
                        {
                            return value;
                        }
                        if (result1 == DialogResult.Yes)
                        {
                            text1 = text4.ToFullString();
                        }
                        else
                        {
                            text1 = text4.ToString();
                        }
                        value = text1;
                    }
                    context.OnComponentChanged();
                }
                catch (Exception exception1)
                {
                    DesignUtil.ReportError(serviceProvider, exception1.Message, null, exception1);
                }
            }
            return value;
        }


        protected override void FillTreeWithData(ObjectSelectorEditor.Selector selector, ITypeDescriptorContext context, IServiceProvider provider)
        {
            if (((selector != null) && (context != null)) && (provider != null))
            {
                this.selector = selector;
                selector.Clear();
                VsConnectionManager manager1 = new VsConnectionManager(provider);
                int num1 = manager1.GetConnectionCount();
                IDbConnection connection1 = context.Instance as IDbConnection;
                ObjectSelectorEditor.SelectorNode node1 = null;
                string text1 = null;
                DbProviderFactory factory1 = null;
                try
                {
                    if (context.Instance != null)
                    {
                        factory1 = ProviderManager.GetFactoryFromType(context.Instance.GetType(), ProviderManager.ProviderSupportedClasses.DbConnection);
                        if (factory1 != null)
                        {
                            text1 = ProviderManager.GetInvariantProviderName(factory1);
                        }
                    }
                }
                catch (Exception)
                {
                }
                for (int num2 = 0; num2 < num1; num2++)
                {
                    string text2 = manager1.GetConnectionString(num2);
                    string text3 = manager1.GetConnectionName(num2);
                    string text4 = manager1.GetProvider(num2);
                    if ((string.IsNullOrEmpty(text1) || string.IsNullOrEmpty(text4)) || string.Equals(text1, text4, 5))
                    {
                        ObjectSelectorEditor.SelectorNode node2 = selector.AddNode(text3, text2, null);
                        if ((connection1 != null) && StringUtil.EqualValue(connection1.ConnectionString, text2))
                        {
                            node1 = node2;
                        }
                    }
                }
                selector.AddNode(SR.GetString("CntEditor_NewConnection"), DbConnectionStringEditor.NEW_CONNECTION_ENTRY, null);
                if (node1 != null)
                {
                    selector.SelectedNode = node1;
                }
            }
        }





        private static Type NEW_CONNECTION_ENTRY;
        protected ObjectSelectorEditor.Selector selector;

       

        protected string ManagedProviderInvariantName
        {
            get
            {
                return ManagedProviderNames.SqlClient;
            }
        }


    }

}
